package member;

import java.util.Map;

import plan.PlanDAO;
import util.MySqlMap;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class MemberAction extends ActionSupport {
	private Member member;
	private String id;

	public String join() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.remove("checked");
		context.setSession(session);
		new MemberDAO().insert(sqlMap, member);
		return SUCCESS;
	}

	public String idcheck() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();

		if( new MemberDAO().select(sqlMap, id) == null){
			addFieldError("idcheckmsg", "使用可能IDです。.");
			session.put("checked", id);
		}
		else{
			addFieldError("idcheckmsg", "既存のIDです。");
			session.remove("checked");
		}
		context.setSession(session);
		return SUCCESS;
	}

	public String login() throws Exception{
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();

		Member login = new MemberDAO().select(sqlMap, member.getId());

		if( login != null && member.getPassword().equals(login.getPassword()) ){
			session.put("loginid", member.getId());
			context.setSession(session);
			return SUCCESS;
		}
		else{
			addFieldError("loginmsg", "IDとパスワードを確認して下さい.");
			return INPUT;
		}

	}

	public String logout() throws Exception{
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.remove("loginid");
		context.setSession(session);
		return SUCCESS;
	}

	public String select() throws Exception{
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		context.setSession(session);
		member = new MemberDAO().select(sqlMap, (String) session.get("loginid"));

		return SUCCESS;
	}

	public String update() throws Exception{
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		new MemberDAO().update(sqlMap, member);
		return SUCCESS;
	}
	public String delete() throws Exception {
		SqlMapClient sqlMap = MySqlMap.getSqlMapInstance();
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		context.setSession(session);
		new PlanDAO().deleteplan(sqlMap, (String) (String) session.get("loginid"));
		new MemberDAO().delete(sqlMap, (String) (String) session.get("loginid"));
		return SUCCESS;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
